﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;
using Microsoft.Data.Extensions;

namespace Basic4Net.BusinessRules
{
    public class SubFactor
    {
        public bool Add(t_SubFactor model, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                db.t_SubFactor.AddObject(model);
                try
                {
                    db.SaveChanges();
                }
                catch
                {
                    msg = "操作失败！";
                    result = false;
                }
                return result;
            }
        }

        public bool Update(t_SubFactor model, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                t_SubFactor model2 = db.t_SubFactor.FirstOrDefault(t => t.SubjectID == model.SubjectID && t.ClsType == model.ClsType);
                model.RID = model2.RID;
                db.ApplyCurrentValues("t_SubFactor", model);
                try
                {
                    db.SaveChanges();
                }
                catch
                {
                    msg = "操作失败";
                    result = false;
                }
                return result;
            }
        }

        public object ListAll()
        {
            using (DBEntities db = new DBEntities())
            {
                var obj0 = from g in db.GLOBALCAUSEINFO
                           join s in db.t_SubFactor on g.RID equals s.SubjectID into temp
                           from t in temp.DefaultIfEmpty()
                           where t.ClsType == null
                           select new
                           {
                               RID = 0,
                               SubjectID = g.RID,
                               CauseName = g.CAUSENAME,
                               ClsType = (int)0,
                               Factor0 = (float)0,
                               Factor1 = (float)0
                           };
                var obj1 = from g in db.GLOBALCAUSEINFO
                          join s in db.t_SubFactor on g.RID equals s.SubjectID into temp
                          from t in temp.DefaultIfEmpty()
                          where t.ClsType ==0
                          select new
                          {
                              RID = t.RID == null ? -1 : t.RID,
                              SubjectID = g.RID,
                              CauseName = g.CAUSENAME,
                              ClsType = (int)t.ClsType == null ? 0 : (int)t.ClsType,
                              Factor0 = (float)t.Factor == null ? 0 : (float)t.Factor,
                              Factor1 = (float)0
                          };

                var obj2 = from g in db.GLOBALCAUSEINFO
                           join s in db.t_SubFactor on g.RID equals s.SubjectID into temp
                           from t in temp.DefaultIfEmpty()
                           where t.ClsType ==1
                           select new
                           {
                               RID = t.RID == null ? -1 : t.RID,
                               SubjectID = g.RID,
                               CauseName = g.CAUSENAME,
                               ClsType = (int)t.ClsType == null ? 1 : (int)t.ClsType,
                               Factor0 = (float)0,
                               Factor1 = (float)t.Factor == null ? 0 : (float)t.Factor
                           };

                var obj5 = obj0.Union(obj1).Union(obj2);

                var obj3 = from t in obj5
                           
                           group t by new
                           {
                               t.SubjectID,
                               t.CauseName
                           } into temp
                               select new
                               {
                                   RID = temp.Sum(z => z.RID),
                                   SubjectID = temp.Key.SubjectID,
                                   CauseName = temp.Key.CauseName,
                                   Factor0 = temp.Sum(z => z.Factor0),
                                   Factor1 = temp.Sum(z => z.Factor1)
                               };

                var obj4 = from t in obj3
                           orderby t.SubjectID
                           select new
                           {
                               RID = t.RID,
                               SubjectID = t.SubjectID,
                               CauseName = t.CauseName,
                               Factor0 = t.Factor0,
                               Factor1 = t.Factor1
                           };
                return obj4.ToList();
            }
        }
    }
}
